home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.12 Dec 96 / Flat File Databases / Invoice Example ƒ / GUI ƒ / Application.c next >
Encoding:
C/C++ Source or Header  |  1996-03-02  |  8.8 KB  |  365 lines  |  [TEXT/CWIE]

  1. // note that comments come from Ed Ringel (ERR) and Apple.  Anything from ERR will be labelled as such
  2.  
  3. #include "BTreeDef.h"
  4. #include "BTreeProtos.h"
  5. #include "StandardFile.h"
  6.  
  7. #include "SampleHeader.h"
  8. #include "InvoiceGlobals.h"
  9. #include "Actions.h"
  10. #include "NOC.h"
  11.  
  12.  
  13. /* In the 68K world, this is provided for you as part of your A5 world, but */
  14. /* on the PowerPC, you have to explicitly include it.                        */
  15. #ifdef __powerc
  16. QDGlobals qd;
  17. #endif
  18.  
  19. /* Two globals.  One always tells me whether I'm in the background or not    */
  20. /* (maintained in the Suspend/Resume event handler in MainEventLoop()), and    */
  21. /* another that allows me to cleanly drop out of my main event loop from     */
  22. /* anywhere.  Sure ExitToShell() works, too, but I like to exit in only one    */
  23. /* way, and this global allows me to do that.*/    
  24. // (ERR) these are in the Global Data.c file
  25.                             
  26.  
  27. /*-------------------------------------------------------------------------------------*/
  28.  
  29. void main()
  30. {
  31.  
  32.     InitApplication();
  33.     PreEventLoop();
  34.     MainEventLoop();
  35. }
  36.  
  37.  
  38. /*-------------------------------------------------------------------------------------*/
  39. /*
  40.     InitApplication - initializes the toolbox, my globals, and my menu bar.
  41. */
  42. void InitApplication()
  43. {
  44.     Handle theMenu;
  45.  
  46.     // Toolbox initialization
  47.     MaxApplZone();
  48.     InitGraf(&qd.thePort);
  49.     InitFonts();
  50.     InitWindows();
  51.     InitMenus();
  52.     TEInit();
  53.     InitDialogs(nil);
  54.     InitCursor();
  55.     FlushEvents(0,everyEvent);
  56.     
  57.     // Application initialization
  58.     gDone = false;
  59.     gInBackground = false;
  60.     
  61.     theMenu = GetNewMBar(kMenuBarResID);
  62.     if ( theMenu != nil )
  63.     {
  64.         SetMenuBar(theMenu);
  65.         AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
  66.         DrawMenuBar();
  67.     }
  68.     else
  69.         /* If the menu stuff failed, something just ain't    */
  70.         /* right (most likely some resources are missing).    */
  71.         gDone = true;
  72.  
  73. }
  74.  
  75.  
  76. /*-------------------------------------------------------------------------------------*/
  77. /*
  78.     DoAboutBox - I just have an Alert dialog box set up in my resources for this.(ERR)
  79. */
  80. void DoAboutBox()
  81. {
  82.     short AlertReturn = Alert(kAboutBoxDITLID, nil);
  83. }
  84.  
  85.  
  86. /*-------------------------------------------------------------------------------------*/
  87. /*
  88.     MainEventLoop - handles all events for this application.  Note that the first
  89.     thing done with an event is to check if it belongs to a dialog, and if so the
  90.     event is passed to DoDialogEvent() which handles all dialog events except
  91.     null events.  To check if it's a dialog event, the IsDialogEvent routine is
  92.     called which checks to see if the frontmost window is a dialog and if the
  93.     event is not an update,  a mousedown or an activate for another window.  Note
  94.     that null events are all passed to DoDialogNullEvent, which is needed to keep
  95.     the editText cursor blinking..
  96.  
  97. */
  98. void MainEventLoop()
  99. {
  100.     EventRecord        theEvent;
  101.     WindowPtr        thisWindow;
  102.     short            clickArea;
  103.     Rect            screenRect;
  104.     long            menuResult;
  105.     char            charCode;
  106.  
  107.     while ( !gDone )
  108.     {
  109.         if ( WaitNextEvent(everyEvent, &theEvent, 0, nil) )
  110.         {
  111.             if ( IsDialogEvent(&theEvent) == true )
  112.             {
  113.                 DoDialogEvent(&theEvent);
  114.                 continue;
  115.             }
  116.  
  117.             switch (theEvent.what)
  118.             {
  119.                 case mouseDown:
  120.                     clickArea = FindWindow(theEvent.where, &thisWindow);
  121.                     
  122.                     switch ( clickArea )
  123.                     {
  124.                         case inDrag:
  125.                             screenRect = (**GetGrayRgn()).rgnBBox;
  126.                             DragWindow(thisWindow, theEvent.where, &screenRect);
  127.                             break;
  128.                         case inContent:
  129.                             if ( thisWindow != FrontWindow() )
  130.                                 SelectWindow(thisWindow);
  131.                             break;
  132.                         case inGoAway:
  133.                             if ( TrackGoAway(thisWindow, theEvent.where) == true )
  134.                             {
  135.                                 if ( ((WindowRecord *)thisWindow)->windowKind == dialogKind )
  136.                                     DisposeDialog(thisWindow);
  137.                                 else
  138.                                     gDone = true;
  139.                             }
  140.                             break;
  141.                         case inMenuBar:
  142.                             menuResult = MenuSelect(theEvent.where);
  143.                             if ( (menuResult  >> 16) != 0 )
  144.                                 (void) MenuCommand(menuResult);
  145.                             break;
  146.                         case inSysWindow:
  147.                             SystemClick(&theEvent,thisWindow);
  148.                             break;
  149.                     }
  150.  
  151.                     break;
  152.                 case keyDown:
  153.                     charCode = theEvent.message & charCodeMask;
  154.  
  155.                     if ( (theEvent.modifiers & cmdKey) != 0 ) 
  156.                     {
  157.                         menuResult = MenuKey(charCode);
  158.                 
  159.                         if ( (menuResult  >> 16) != 0 )
  160.                             (void) MenuCommand(menuResult);
  161.                             
  162.                     } 
  163.                     break;
  164.                 case updateEvt:    
  165.                     break;
  166.                 case diskEvt:
  167.                 // added by ERR; file oriented  program
  168.                     if (HiWord(theEvent.message) != noErr) {
  169.                         Point diskInitPt = {0,0};
  170.                         DILoad();
  171.                         DIBadMount(diskInitPt, theEvent.message);
  172.                         DIUnload();
  173.                     }                
  174.                     break;
  175.                 case app4Evt: 
  176.                     /* Make sure the SIZE resource reflects that suspend evts are accepted.*/
  177.                     if ( theEvent.message & 0x01000000 )         /* Suspend or resume? */
  178.                     {
  179.                         if ( theEvent.message & 0x00000001 )/* Is it a resume event? */
  180.                         {
  181.                             gInBackground = false;
  182.                             SetCursor(&qd.arrow);
  183.                         }
  184.                         else                                 /* It's a suspend event */
  185.                             gInBackground = true;
  186.  
  187.                     }
  188.                     break;
  189.             }
  190.         }
  191.         else
  192.             DoDialogNullEvent(&theEvent);
  193.     }
  194. }
  195.  
  196.  
  197.  
  198. /*-------------------------------------------------------------------------------------*/
  199. /*
  200.     MenuCommand - called in response to keydowns in both my dialog event handler and
  201.     the main event loop.  Note that because of AdjustMenus(), the edit keys will only
  202.     be hit if the dialog is the frontmost window, which is what we want.
  203.     
  204.  
  205.  
  206. */
  207.  
  208. #define kActivate 1
  209. #define kInactivate 2
  210.  
  211.  
  212.  
  213.  
  214. Boolean MenuCommand(long whaHappened)
  215. {
  216.     short        menuID, menuItem;
  217.     Boolean     performedEdit = false;
  218.     DialogPtr    theDialog;
  219.     short        menuErr;
  220.     
  221.     menuID = (whaHappened >> 16);
  222.     menuItem = (whaHappened & 0xFFFF);
  223.     
  224.     switch ( menuID )
  225.     {
  226.         case kAppleMenuID:
  227.             HiliteMenu(kAppleMenuID);
  228.             if ( menuItem == 1)
  229.                 DoAboutBox();
  230.             break;
  231.  
  232.         case kFileMenuID:
  233.             HiliteMenu(kFileMenuID);
  234.             switch ( menuItem )
  235.             {
  236.                 case kNewItem:
  237.                     menuErr = NewFile();
  238.                     if (menuErr) {
  239.                         HandleErrorMsg(menuErr);
  240.                         break;
  241.                     }
  242.                     DisableItem(gFileMenu,kNewItem);
  243.                     DisableItem(gFileMenu,kOpenItem);
  244.                     EnableItem(gFileMenu,kCloseItem);
  245.                     ToggleControl(gCustomerDialog,1,kActivate);
  246.                     ToggleControl(gPartDialog,1,kActivate);
  247.                     ToggleControl(gInvoiceDialog,1,kActivate);
  248.                     break;
  249.                 case kOpenItem:
  250.                     menuErr = OpenFile();
  251.                     if (menuErr) {
  252.                         HandleErrorMsg(menuErr);
  253.                         break;
  254.                     }
  255.                     DisableItem(gFileMenu,kNewItem);
  256.                     DisableItem(gFileMenu,kOpenItem);
  257.                     EnableItem(gFileMenu,kCloseItem);
  258.                     ToggleControl(gCustomerDialog,1,kActivate);
  259.                     ToggleControl(gPartDialog,1,kActivate);
  260.                     ToggleControl(gInvoiceDialog,1,kActivate);
  261.                     break;
  262.  
  263.                 case kCloseItem:
  264.                     menuErr = CloseFile();
  265.                     if (menuErr) {
  266.                         HandleErrorMsg(menuErr);
  267.                         break;
  268.                     }
  269.                     EnableItem(gFileMenu,kNewItem);
  270.                     EnableItem(gFileMenu,kOpenItem);
  271.                     DisableItem(gFileMenu,kCloseItem);
  272.                     ToggleControl(gCustomerDialog,1,kInactivate);
  273.                     ToggleControl(gPartDialog,1,kInactivate);
  274.                     ToggleControl(gInvoiceDialog,1,kInactivate);
  275.                     break;
  276.  
  277.                 case kQuitItem:
  278.                     gDone = true;
  279.                     if (gFileIsOpen)
  280.                         menuErr = CloseFile();
  281.                     break;
  282.             }
  283.             break;
  284.         case kEditMenuID:
  285.             // This thing should be a dialog, because the menu items are
  286.             // disabled unless a modeless dialog is frontmost.
  287.             theDialog = (DialogPtr)FrontWindow();
  288.             
  289.             if ( theDialog != nil )
  290.             {
  291.                 HiliteMenu(kEditMenuID);            
  292.                 switch ( menuItem )
  293.                 {
  294.                     case kCutItem:
  295.                         DoCut(theDialog);
  296.                         performedEdit = true;
  297.                         break;
  298.                     case kCopyItem:
  299.                         DoCopy(theDialog);
  300.                         performedEdit = true;
  301.                         break;
  302.                     case kPasteItem:
  303.                         DoPaste(theDialog);
  304.                         performedEdit = true;
  305.                         break;
  306.                     case kClearItem:
  307.                         DoClear(theDialog);
  308.                         performedEdit = true;
  309.                         break;
  310.                 }//switch
  311.             }//if
  312.             break;
  313.         case kWindowMenuID:
  314.             switch ( menuItem) {
  315.                 case kCustomerWindowID:
  316.                     SelectWindow(gCustomerDialog);
  317.                     break;
  318.                 case kPartWindowID:
  319.                     SelectWindow(gPartDialog);
  320.                     break;
  321.                 case kInvoiceWindowID:
  322.                     SelectWindow(gInvoiceDialog);
  323.                     break;
  324.                 case kPictureWindowID:
  325.                         HandlePictureDialog();
  326.                     break;
  327.             }
  328.             break;
  329.     }
  330.     HiliteMenu(0);
  331.     return performedEdit;
  332. }
  333.  
  334.  
  335.  
  336.  
  337. /*-------------------------------------------------------------------------------------*/
  338.  
  339. /*-------------------------------------------------------------------------------------*/
  340.  
  341. void PreEventLoop(void)
  342. {
  343.     //CreateNewWindow();
  344.     CreateModelessDialog(kCustomerDLGID,&gCustomerDialog);    
  345.     CreateModelessDialog(kPartDLGID,&gPartDialog);    
  346.     CreateModelessDialog(kInvoiceDLGID,&gInvoiceDialog);    
  347.     gFileMenu = GetMenuHandle(kFileMenuID);
  348.     gEditMenu = GetMenuHandle(kEditMenuID);
  349.     gWindowMenu = GetMenuHandle(kWindowMenuID);
  350.     ToggleControl(gCustomerDialog,1,kInactivate);
  351.     ToggleControl(gPartDialog, 1, kInactivate);
  352.     ToggleControl(gInvoiceDialog,1,kInactivate);
  353. }
  354.  
  355.  
  356. /*-------------------------------------------------------------------------------------*/
  357.  
  358. void PostEventLoop(void)
  359. {
  360. }
  361.  
  362.  
  363. /*-------------------------------------------------------------------------------------*/
  364.  
  365.